package apibasej.db.transaction;

import apibasej.db.metadata.Col;
import apibasej.db.metadata.Tab;
import apibasej.db.sql.wrapper.ColSelect;
import apibasej.db.sql.wrapper.Entry;
import apibasej.db.sql.wrapper.Filter;
import apibasej.db.sql.wrapper.SQLWrapperException;
import apibasej.db.sql.wrapper.WrapperTab;
/**
 * http://code.google.com/p/apibasej/
 * 
 * @author Julio Azevedo Lessa - apibasej@gmail.com
 * 
 */
// Evitar usar esta classe sempre que possível. É limitada, não suporta select com joins e pode gerar erros para operações mais complexas.
public class UtilDBStr extends UtilDB{

	public UtilDBStr(String t){
		super(new Tab(null, t, 0, false));
		setUseAsterisk(true);
	}
	public UtilDBStr(){
		super(new WrapperTab(null));
		setUseAsterisk(true);
	}

	
	
	protected WrapperTab getWrapperTabAddedForCol(Col<?> col) throws SQLWrapperException {
		if(col == null) return null;
		else return getWrapperSQL().getWrapperTabAddedForCol(col);
	}
	
	private <T> Col<T> getCol(String nameCol, Class<T> typeCol) throws SQLWrapperException{
		if(nameCol==null) return null;
		return new Col<T>(getWrapperSQL().getWrapperTabRoot().getTab(), nameCol, typeCol);
	}
	/*
	private TabGroup tg;
	public TabGroup getTabGroup() {
		if(tg==null){
			tg = getWrapperSQL().getTab().getTab().getTabGroup();
		}
		return tg;
	}
	@SuppressWarnings("unchecked")
	private <T> Col<T> getCol(String nameCol, Class<T> typeCol) throws SQLWrapperException{
		if(nameCol==null) return null;
		if(getTabGroup().getTabs().size()!=1) throw new UtilDBException("UtilDBStr permit only one table");
		Tab t = getTabGroup().getTabs().values().iterator().next();
		if(t==null) throw new UtilDBException("Table not found");
		Col<T> c = (Col<T>)t.getCol(nameCol);
		if(c==null) {
			c = new Col<T>(t, nameCol, typeCol);
			t.getCols().put(nameCol, c);
		}
		return c;
	}*/
	
	public <T> Filter<T> addFilter(String nameCol, T val, Class<T> typeCol) throws SQLWrapperException{
		return super.getWhere().addFilter(getCol(nameCol, typeCol), val);
	}
	public Filter<?> addFilter(Object val) throws SQLWrapperException{
		return addFilter(null, val, Object.class);
	}
	public Filter<?> addFilter(String nameCol, Object val) throws SQLWrapperException{
		return addFilter(nameCol, val, Object.class);
	}
	public <T> ColSelect<T> addColSelect(String nameCol, Class<T> typeCol) throws SQLWrapperException {
		return super.addColSelect(getCol(nameCol, typeCol));
	}
	public ColSelect<?> addColSelect(String nameCol) throws SQLWrapperException {
		return super.addColSelect(getCol(nameCol, Object.class));
	}
	public <T> Entry<T> addEntry(String nameCol, T value, Class<T> typeCol) throws SQLWrapperException {
		return super.addEntry(getCol(nameCol, typeCol), value);
	}
	public Entry<?> addEntry(String nameCol, Object val) throws SQLWrapperException {
		return super.addEntry(getCol(nameCol, Object.class), val);
	}
	
	
	
	
	
}
